package com.dreamlin.basepro.base.view.adapter;

import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class BaseAdapter<T> extends RecyclerView.Adapter<BaseAdapter.BaseViewHolder<T>> implements View.OnClickListener {

    @LayoutRes
    protected int layoutId;

    protected List<T> data;

    protected AdapterListener<T> listener;
    protected ItemClickListener<T> itemClickListener;

    public BaseAdapter(int layoutId, List<T> data) {
        this.layoutId = layoutId;
        this.data = data;
    }

    public void setListener(AdapterListener<T> listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public BaseViewHolder<T> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(layoutId, parent, false);
        if (itemClickListener != null)
            view.setOnClickListener(this);
        return new BaseViewHolder<T>(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder<T> holder, int position) {
        holder.itemView.setTag(position);
        if (listener != null) {
            listener.convert(holder, data.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        if (itemClickListener != null) {
            itemClickListener.onItemViewClick(v, data.get(position), position);
        }
    }

    public static class BaseViewHolder<B> extends RecyclerView.ViewHolder {
        private SparseArray<View> views = new SparseArray<>();

        public BaseViewHolder(@NonNull View itemView) {
            super(itemView);
        }

        public <V extends View> V getView(@IdRes int viewId) {
            View view = views.get(viewId);
            if (view == null) {
                view = itemView.findViewById(viewId);
                views.put(viewId, view);
            }
            return (V) view;
        }

        public void setText(@IdRes int id, @NonNull String text) {
            View textView = views.get(id);
            if (textView != null) {
                if (textView instanceof TextView) {
                    ((TextView) textView).setText(text);
                }
            } else {
                View viewById = itemView.findViewById(id);
                if (viewById != null) {
                    views.put(id, viewById);
                    if (viewById instanceof TextView) {
                        ((TextView) viewById).setText(text);
                    }
                }
            }
        }
    }

    public interface AdapterListener<B> {
        void convert(BaseViewHolder<B> holder, B bean);
    }
}
